<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: show_stats.tpl.php 1151 2008-06-24 15:07:32Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 */

// This template provides three output formats:

// 1.) it is the data output for Flash Chart with season statistic of a league
// 2.) it's a usual joomla content page to display season statistic of a league

// check for first output option
if ($jl_func == "showstats_getchartdata")
{
  // Calculate Values for Chart Object
  $homeSum = array();
  $awaySum = array();
  $matchDayGoalsCount = array();
  $label = '';
  $acYLabel = 1;
  
  $matchDayGoalsCountMax = 0;
  foreach( $rows as $rw )
  {
    // Only draw until current matchday
    if ( $rw->matchcode > $actualround ) {
      $NoSumAvail = true;
    }
    if (!$rw->homegoalspd) $rw->homegoalspd = 0;
    if (!$rw->guestgoalspd) $rw->guestgoalspd = 0;
    $homeSum[] = $rw->homegoalspd;
    $awaySum[] = $rw->guestgoalspd;
    if ($NoSumAvail)
    {
      $matchDayGoalsCount[] = 'null';
    }
    else
    {
      $matchDayGoalsCount[] = $rw->homegoalspd + $rw->guestgoalspd;
      if ($matchDayGoalsCountMax < ($rw->homegoalspd + $rw->guestgoalspd)) {
        $matchDayGoalsCountMax = ($rw->homegoalspd + $rw->guestgoalspd);
      }
    }
    if ($acYLabel > 1) $label .= ',';
    $label .= $acYLabel;
    $acYLabel++;
  }

  // Generate RenderCommands for Chart Object

  include( JL_FRONTEND_PATH.'/lib/open-flash-chart/open-flash-chart.php' );
  $g = new graph();
  // backgound color of flash statistic
  $g->bg_colour = $overall_config['bg_colour'];
  // inner background color with gradient
  $g->set_inner_background( $overall_config['inner_bg_gradient_1'], $overall_config['inner_bg_gradient_2'], $overall_config['inner_bg_gradient_orientation'] );
  
  if ($showStats_config['home_away_stats']) 
  {
	  // data 1: home goals
	  $g->set_data( $homeSum );
	  // first bar
	  $g->bar( 50, $overall_config['bar1'], _JL_STATS_HOME, 10 );
	  // data 2 : away goals
	  $g->set_data( $awaySum );
	  $g->bar( 50, $overall_config['bar2'], _JL_STATS_AWAY, 10 );
  }
  // data 3 : total goals
  $g->set_data( $matchDayGoalsCount );
  $g->line_dot( 3, 5, $overall_config['bar3'], _JL_STATS_TOTAL, 10 );    // <-- 3px thick + dots

  $g->set_x_labels( array( $label ) );
  // well, general formula: $givenmaxval - $givenmaxval % $wishedcountofsteps + $wishedcountofsteps
  // if you want no space to top if number is divisibly by $wishedcountofsteps you must not add
  // $wishedcountofsteps ... like:
  // $myset_y_max = (($givenmaxval % $wishedcountofsteps)==0) ? $givenmaxval - $givenmaxval % $wishedcountofsteps : $givenmaxval - $givenmaxval % $wishedcountofsteps + $wishedcountofsteps;
  // $g->set_y_max( $myset_y_max );
  // manygreetz, carsten :-)
  $g->set_y_max( $matchDayGoalsCountMax + 5 - $matchDayGoalsCountMax % 5 );
  $g->y_label_steps( 5 );
  // x-axis color (color for bottom x-axis, color for inner x-axis)
  $g->x_axis_colour( $overall_config['x_axis_colour'], $overall_config['y_axis_colour_inner'] );
  // y-axis color (color for left y-axis, color for inner y-axis)
  $g->y_axis_colour( $overall_config['y_axis_colour'], $overall_config['x_axis_colour_inner'] );
  $g->set_tool_tip( _JL_STATS_GOALS.': #val#' );  
  echo  $g->render(); // Method doesn't really render, but it returns a drawlist as multiline text string
  exit();
}

// ELSE (second output option):

  // Show Content of ShowStats Template
?>

<?php
// Set display style dependent on what stat parts should be shown
$show_general = 'none';
if ($showStats_config['show_general_stats'] == "1") {
	$show_general = 'inline';
}
$show_goals = 'none';
if ($showStats_config['show_goals_stats'] == "1") {
	$show_goals = 'inline';
}
$show_attendance = 'none';
if ($showStats_config['show_attendance_stats'] == "1") {
	$show_attendance = 'inline';
}
$show_flash = '';
if ($showStats_config['show_goals_stats_flash'] == "0") {
	$show_flash = 'display:none;';
}
$show_att_ranking = '';
if ($showStats_config['show_attendance_ranking'] == "0") {
$show_att_ranking = 'display:none;';
}
$show_events = '';
if ($showStats_config['show_events_stats'] == "0") {
	$show_events = 'display:none;';
}
?>
<div id="joomleague">
<!-- Header with Project Name-->
<?php if ($showStats_config['show_project_heading'] == "1" OR $overall_config['show_project_heading'] == "1") { ?>
<div class="componentheading" <?php if ($showStats_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo 'style="float:left;"';?>><?php echo $htmlHelper->showTitle($joomleague, $overall_config['project_heading_type']); ?></div>
<!-- Druckfunktion -->
<?php if ($showStats_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") {
$htmlHelper->PrintIcon( $row, $params, false, '' );
};?>
<!-- Druckfunktion Ende -->
<?php } ?>
<!-- Header END -->

<!-- sectionheader -->
<table width="100%" class="contentpaneopen">
  <tr>
    <td class="contentheading">
      <?php echo _JL_STATS;?>
    </td>
  </tr>
</table>
<!-- sectionheader ends -->

<?php 
if (   $showStats_config['show_general_stats'] == "1" 
    OR $showStats_config['show_goals_stats'] == "1" 
    OR $showStats_config['show_attendance_stats'] == "1") 
{ 
	?>
	<br />
  <div style="display:<?php echo $show_general;?>;float:left;width:48%;margin:0 10px 15px 0;">
    <table cellspacing="0">
      <tr><td class="sectiontableheader" colspan="2"><?php echo _JL_STATS_GENERAL;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_MATCHDAYS;?>:</td><td style="width:40%;text-align:right; padding:0 10px 0 10px;"> <?php echo $totalrounds;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_CURRENT_MATCHDAY;?>:</td><td style="text-align:right; padding:0 10px 0 10px;"> <?php echo $actualround;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_MATCHES ." / ". _JL_TABLE_MD_SHORT;?>:</td><td style="text-align:right; padding:0 10px 0 10px;"> <?php echo  round (($totals[0]->totalmatches / $totalrounds),2);?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_MATCHES_OVERALL;?>:</td><td style="text-align:right; padding:0 10px 0 10px;"><?php echo $totals[0]->totalmatches;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_MATCHES_PLAYED;?>:</td><td style="text-align:right; padding:0 10px 0 10px;"> <?php echo $totals[0]->playedmatches;?></td></tr>
      <?php 
      if ($showStats_config['home_away_stats']) 
      {
	      ?>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><b><?php echo _JL_STATS_MATCHES_HIGHEST_WON_HOME;?>:</b><br /> <?php echo $highest_home->hometeam." - ".$highest_home->guestteam. "</td><td style=\"white-space:nowrap;text-align:right;padding:0 10px 0 10px;\"><br />".$highest_home->homegoals." : ".$highest_home->guestgoals;?></td></tr>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><b><?php echo _JL_STATS_MATCHES_HIGHEST_WON_AWAY;?>:</b><br /> <?php echo $highest_away->hometeam." - ".$highest_away->guestteam. "</td><td style=\"white-space:nowrap;text-align:right;padding:0 10px 0 10px;\"><br />".$highest_away->homegoals." : ".$highest_away->guestgoals;?></td></tr>
	      <?php 
      }
      else 
      {
        if ( ($highest_home->homegoals - $highest_home->guestgoals) > ($highest_away->guestgoals - $highest_away->homegoals)) {
            $highest = $highest_home;
        }
        else {
          $highest = $highest_away;
        }
        ?>
        <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><b><?php echo _JL_STATS_MATCHES_HIGHEST_WIN;?>:</b><br /> <?php echo $highest->hometeam." - ".$highest->guestteam. "</td><td style=\"white-space:nowrap;text-align:right;padding:0 10px 0 10px;\"><br />".$highest->homegoals." : ".$highest->guestgoals;?></td></tr>
        <?php 
      } 
      ?>
    </table>
  </div>

  <div style="display:<?php echo $show_goals;?>;float:left;width:48%;margin:0 10px 15px 0;">
    <table cellspacing="0">
      <tr><td class="sectiontableheader" colspan="2"><?php echo _JL_STATS_GOALS;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_TOTAL;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php echo $totals[0]->sumgoals;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_TOTAL_PER_MATCHDAY;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 10px;"> <?php if ($totals[0]->playedmatches>0) echo round ((($totals[0]->sumgoals / $totals[0]->playedmatches) * ($totals[0]->totalmatches / $totalrounds)),2); else echo '0';?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_TOTAL_PER_MATCH;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php if ($totals[0]->playedmatches>0) echo round (($totals[0]->sumgoals / $totals[0]->playedmatches),2); else echo '0'; ?></td></tr>
      <?php if ($showStats_config['home_away_stats']) {?>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_HOME;?></td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php echo $totals[0]->homegoals;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_HOME_PER_MATCHDAY;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php if ($totals[0]->playedmatches>0) echo round((($totals[0]->homegoals / $totals[0]->playedmatches) * ($totals[0]->totalmatches / $totalrounds)),2); else echo '0';?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_HOME_PER_MATCH;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php if ($totals[0]->playedmatches>0) echo round(($totals[0]->homegoals / $totals[0]->playedmatches),2); else echo '0'; ?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_AWAY;?></td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php echo $totals[0]->guestgoals;?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_AWAY_PER_MATCHDAY;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php if ($totals[0]->playedmatches>0) echo round((($totals[0]->guestgoals / $totals[0]->playedmatches) * ($totals[0]->totalmatches / $totalrounds)),2); else echo '0'; ?></td></tr>
      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_GOALS_AWAY_PER_MATCH;?>:</td><td style="width:40%; text-align:right; padding:0 6px 0 6px;"> <?php if ($totals[0]->playedmatches>0) echo round(($totals[0]->guestgoals / $totals[0]->playedmatches),2); else echo '0';?></td></tr>
      <?php } ?>
    </table>
   </div>

    <?php 
    if ($showStats_config['show_attendance_stats']) 
    {
	    ?>
	    <div style="display:<?php echo $show_attendance;?>;float:left;width:48%;margin:0 10px 15px 0;">
	    <table cellspacing="0">
	   	  <tr><td class="sectiontableheader" colspan="2" width="50%"><?php echo _JL_STATS_ATTENDANCE;?></td></tr>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_ATTENDANCE_TOTAL;?>:</td><td style="width:40%; text-align:right;"> <?php echo $totals[0]->sumspectators;?></td></tr>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><?php echo _JL_STATS_ATTENDANCE_PER_MATCH;?>:</td><td style="width:40%; text-align:right;"> <?php echo round (($totals[0]->sumspectators / $totals[0]->attendedmatches),2);?></td></tr>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><b><?php echo _JL_STATS_ATTENDANCE_BEST_AVG;?>:</b><br /><?php echo $bestavgteam;?></td><td style="width:40%; text-align:right;"><br /> <?php echo $bestavg;?></td></tr>
	      <tr class="sectiontableentry1"><td style="white-space:nowrap;width:60%;"><b><?php echo _JL_STATS_ATTENDANCE_WORST_AVG;?>:</b><br /><?php echo $worstavgteam;?></td><td style="width:40%; text-align:right;"><br /><?php echo $worstavg;?></td></tr>
	    </table>     
	    </div>
	    <?php 
    } 
    ?>
		<br />
		<?php 
  }
  
	if ($showStats_config['show_goals_stats_flash']) 
	{
    ?> 
		<!-- Flash Statistik Start -->
		<div style="<?php echo $show_flash;?>float:left;width:96%;clear:both;margin:25px 0 25px 0">
		<table width="100%">
			<tr class="sectiontableheader">
		    <td class="sectiontableheader"><?php echo _JL_STATS_GOALS_STATISTIC;?></td>
		  </tr>
		  <tr><td></td></tr>
		  <tr>
		    <td style="text-align:center;">
		      <?php $htmlHelper->open_flash_chart_object( '100%', 250, _JOOMLA_JL_LIVE_SITE."/".str_replace('&amp;', '%26', JL_COMPONENT_LINK.JL_ITEMID_SUFFIX)."%26func=showStats_GetChartData%26p=".$p );?>
		    </td>  
		  </tr>
		</table>
		</div>
		<!-- Flash Statistik END -->
		<br />
		<?php
	}  

	if ( $showStats_config['show_attendance_ranking'] ) 
	{
		?>
		<div style="<?php echo $show_att_ranking;?>float:left;width:96%;clear:both;margin:0px 0 25px 0">
		<table width="100%" class="contentpaneopen">
		  <tr>
		    <td class="contentheading">
		      <?php echo _JL_STATS_ATTENDANCE_RANKING;?>
		    </td>
		  </tr>
		</table>
		<br />
		<table width="100%" cellspacing="0">
			<tr>
			<td class="sectiontableheader" style="width:6px;"></td>
			<td class="sectiontableheader" style="width:25%;"><?php $htmlHelper->print_column_heading_sort( _JL_STATS_ATTENDANCE_RANKING_TEAM , "name", $table_config, 'ASC');?></td>
			<td class="sectiontableheader" style="width:3%;text-align:center;"><?php echo _JL_STATS_ATTENDANCE_RANKING_MATCHES;?></td>
			<td class="sectiontableheader" style="width:16%;text-align:right;"><?php $htmlHelper->print_column_heading_sort( _JL_STATS_ATTENDANCE_RANKING_TOTAL , "totalattend", $table_config);?></td>
			<td class="sectiontableheader" style="width:16%;text-align:right;"><?php $htmlHelper->print_column_heading_sort( _JL_STATS_ATTENDANCE_RANKING_AVG , "avgattend", $table_config);?></td>
			<td class="sectiontableheader" style="width:19%;text-align:right;"><?php $htmlHelper->print_column_heading_sort( _JL_STATS_ATTENDANCE_RANKING_CAPACITY , "capacity", $table_config);?></td>
			<td class="sectiontableheader" style="width:20%;text-align:right;"><?php $htmlHelper->print_column_heading_sort( _JL_STATS_ATTENDANCE_RANKING_UTILISATION , "utilisation", $table_config);?></td>
			</tr>
		<?php
			$htmlHelper->show_attendance_ranking($attendance_ranking, $table_config);
		?>
		</table>
		</div>
		<?php 	
	}
	 
  if ( $showStats_config['show_events_stats'] )
  {
	  ?> 
		<div style="<?php echo $show_events;?>float:left;width:96%;clear:both;margin:0 0 25px 0">
		<table width="100%" class="contentpaneopen">
		  <tr>
		    <td class="contentheading">
		      <?php if (isset($eventtype) && $eventtype>0) { echo $eventtypes[$eventtype]->name; } else echo _JL_STATS_EVENTS;?>
		    </td>
		  </tr>
		</table>
		<br />
		<table width="100%" align="center" border="0" cellspacing="0">
		  <tr>
		    <td><?php $htmlHelper->show_event_stats( $eventtypes, $eventrankings, $events_config );?></td>  
		  </tr>
		</table>
		</div>
		<br />
		<?php
  }
  ?>

<!-- Print Button START -->
<?php if ($showStats_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($showStats_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = & new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
  echo "</div>";
};?>
<!-- Print Button END -->
</div>
